package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.utils.PatternUtil;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerLoginComponent;
import com.eduhzy.ttw.teacher.mvp.contract.LoginContract;
import com.eduhzy.ttw.teacher.mvp.presenter.LoginPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.xw.repo.XEditText;

import org.simple.eventbus.Subscriber;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

import static com.eduhzy.ttw.commonsdk.core.Constants.TEACHER;
import static com.jess.arms.utils.Preconditions.checkNotNull;

@Route(path = RouterHub.APP_LOGINACTIVITY)
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.login_logo)
    ImageView loginLogo;
    @BindView(R.id.login_version)
    TextView loginVersion;
    @BindView(R.id.login_user)
    XEditText loginUser;
    @BindView(R.id.login_pwd)
    XEditText loginPwd;
    private Reference<LoginActivity> reference;
    private String username;
    private String password;
    private QMUITipDialog loading;
    private int loginType;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

        DaggerLoginComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        try {
            reference = new WeakReference(this);
            loginVersion.setText("教师版  v " + AppUtils.getAppVersionName());
            username = SPUtils.getInstance().getString(Constants.USERNAME);
            password = SPUtils.getInstance().getString(Constants.PASSWORD);
            if (ObjectUtils.isNotEmpty(username)) {
                loginUser.setText(username);
                loginUser.clearFocus();
            }
//            if (ObjectUtils.isNotEmpty(password)) {
//                loginPwd.setText(password);
//                loginPwd.clearFocus();
//            }
        } catch (Exception e) {
            killMyself();
        }
    }

    @Override
    public void showLoading() {
        if (loading == null) {
            loading = new QMUITipDialog.Builder(this)
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord("正在登录···")
                    .create(false);
        }
        loading.show();
    }

    @Override
    public void hideLoading() {
        if (loading != null) {
            loading.dismiss();
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Subscriber(tag = EventBusTags.LOGIN)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.LOGIN_SUCCESS:
                SPUtils.getInstance().put(Constants.LOGIN_TYPE, loginType);
                killMyself();
                break;
            default:
        }
    }

    @OnClick({R.id.login_sign_in, R.id.login_register, R.id.login_forget, R.id.login_qq, R.id.login_wechat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_sign_in:
                gotoLogin();
                break;
            case R.id.login_register:
                ARouter.getInstance()
                        .build(RouterHub.APP_REGISTERACTIVITY)
                        .withString(Intent.EXTRA_TITLE, ((TextView) view).getText().toString())
                        .withInt(Constants.LOGIN_TYPE, Constants.LOGIN_NORMAL)
                        .navigation();
                break;
            case R.id.login_forget:
                Utils.navigationWithTitle(RouterHub.APP_FORGETPWDACTIVITY, Constants.FIND_PASSWORD);
                break;
            case R.id.login_qq:
                this.loginType = Constants.LOGIN_THIRD_QQ;
                mPresenter.thirdLogin(QQ.NAME, Constants.LOGIN_THIRD_QQ);
                break;
            case R.id.login_wechat:
                this.loginType = Constants.LOGIN_THIRD_WX;
                mPresenter.thirdLogin(Wechat.NAME, Constants.LOGIN_THIRD_WX);
                break;
            default:
        }
    }

    private void gotoLogin() {

        KeyboardUtils.hideSoftInput(reference.get());
        String user = loginUser.getText().toString();
        String pwd = loginPwd.getText().toString();
        if (StringUtils.isTrimEmpty(user) || !PatternUtil.isMobilePhone(user)) {
            RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请输入正确的手机号码！", 1500);
            return;
        }
        if (StringUtils.isTrimEmpty(pwd) || !PatternUtil.isContainNumAndLetter(pwd)) {
            RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "密码必须同时包含字母和数字,且最少有8位！", 1500);
            return;
        }
        mPresenter.login(user, pwd, TEACHER);
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
